
#include <stdlib.h>
#include "lcd.h"
#include "input_ts.h"

//棋盘左上顶点的坐标
#define MATRIX_X0  0
#define MATRIX_Y0  0


//棋子宽度
#define BOARD_SIZE 80


//棋子与棋子之间间隔
#define BLANK_SIZE 5

//每一行棋子的数量
#define BOARD_NUM 4


//2048游戏一个核心数组
int matrix_2048[BOARD_NUM][BOARD_NUM] = 
{
	0,0,0,0,
	0,0,0,0,
	0,0,0,0,
	0,0,0,0
};


char *get_bmpname_by_digtal(int num)
{
	if (num == 2)
	{
		return "color_x80_2.bmp";
	}
	else if (num == 4)
	{
		return "color_x80_4.bmp";
	}
	else if (num == 8)
	{
		return "color_x80_8.bmp";
	}
	else if (num == 16)
	{
		return "color_x80_16.bmp";
	}
	else if (num == 32)
	{
		return "color_x80_32.bmp";
	}
	else if (num == 64)
	{
		return "color_x80_64.bmp";
	}
	else if (num == 128)
	{
		return "color_x80_128.bmp";
	}
	else if (num == 256)
	{
		return "color_x80_256.bmp";
	}
	else if (num == 512)
	{
		return "color_x80_512.bmp";
	}
	else if (num == 1024)
	{
		return "color_x80_1024.bmp";
	}
	else if (num == 2048)
	{
		return "color_x80_2048.bmp";
	}
}

int get_zero_num(void)
{
	int i,j;
	int num = 0;
	for (i = 0; i < BOARD_NUM; i++)
	{

		for (j = 0; j < BOARD_NUM ; j++)
		{

			if (matrix_2048[i][j] == 0)
			{
				num++;
			}
		}
	}

	return num;
}

void fill_a_digtal_to_matrix(void)				//随机填一个数字放到16个小方块中
{
	int zero_num  = get_zero_num();//矩阵中0的个数

	
	if (zero_num == 0)		//如果矩阵中一个0的位置都没有就返回
	{
		return ;
	}


	int pos = random() % zero_num; //随机的0的编号的位置
				//按行数，第pos个0就是要填充的位置

	
	int i,j;
	int n = 0;
	for (i = 0; i < BOARD_NUM; i++)
	{

		for (j = 0; j < BOARD_NUM ; j++)
		{

			if (matrix_2048[i][j] == 0)	//首先要判断这个二维矩阵中这个位置为0才能插入一个数
			{
				if (n == pos)
				{
					//
					int a[] = {2, 4,2, 2};			//随机值数组，2的概率为百分之75，4的概率为百分之25。
					
					matrix_2048[i][j] = a[ random() % 4];
					return ;
				}
				n++;
			}
		}
	}

}



void lcd_draw_matrix(void)
{
	int i,j;

	for (i = 0; i < BOARD_NUM; i++)
	{

		for (j = 0; j < BOARD_NUM; j++)
		{
			if (matrix_2048[i][j] == 0)		//	//棋子与棋子之间间隔 BLANK_SIZE 5//棋子宽度BOARD_SIZE 80													
			{
				lcd_draw_rectangle(MATRIX_X0 +(BOARD_SIZE + BLANK_SIZE)*j , 		//X0
							MATRIX_Y0 +(BOARD_SIZE + BLANK_SIZE)*i ,  				//y0
							BOARD_SIZE, BOARD_SIZE, 								//w and h
							0xff0000);												//yanse
			}
			else
			{
				lcd_display_bmp(MATRIX_X0 +(BOARD_SIZE + BLANK_SIZE)*j , 
							MATRIX_Y0 +(BOARD_SIZE + BLANK_SIZE)*i,
							get_bmpname_by_digtal( matrix_2048[i][j]));
			}
		}
	}
	


}



//游戏结束的标志，1 -> 结束　　0 ->不结束
int game_over = 0;


void move_left(void)
{
	//向左滑动的矩阵变换算法

	int i, j;
	int value, l ;

	for (i = 0; i < BOARD_NUM;i++)
	{

		value = 0;
		l = 0;
		for (j = 0; j <BOARD_NUM; j++)
		{
			if (matrix_2048[i][j] != 0)
			{
				if (value == 0)
				{
					// j直接取代value
					value = matrix_2048[i][j];
					matrix_2048[i][j] = 0;
				}
				else //value != 0
				{
					if (matrix_2048[i][j] == value)
					{
						value += matrix_2048[i][j];
						matrix_2048[i][l++] = value;
						value=0;
						matrix_2048[i][j] = 0;
					}
					else
					{
						//value前移，j取代value
						matrix_2048[i][l++] = value;
						value = matrix_2048[i][j];
						matrix_2048[i][j] = 0;
					}
				}
			}
		}

		if (value != 0)
		{
			matrix_2048[i][l++] = value;
		}
			
	}


	
}
void move_right(void)
{ 	
	//向左滑动的矩阵变换算法

	int i, j;
	int value, l ;

	for (i = 0; i < BOARD_NUM;i++)
	{

		value = 0;
		l = BOARD_NUM-1;
		for (j = BOARD_NUM-1; j >=0; j--)
		{
			if (matrix_2048[i][j] != 0)
			{
				if (value == 0)
				{
					// j直接取代value
					value = matrix_2048[i][j];
					matrix_2048[i][j] = 0;
				}
				else //value != 0
				{
					if (matrix_2048[i][j] == value)
					{
						value += matrix_2048[i][j];
						matrix_2048[i][l--] = value;
						value=0;
						matrix_2048[i][j] = 0;
					}
					else
					{
						//value前移，j取代value
						matrix_2048[i][l--] = value;
						value = matrix_2048[i][j];
						matrix_2048[i][j] = 0;
					}
				}
			}
		}

		if (value != 0)
		{
			matrix_2048[i][l--] = value;
		}
			
	}

}
	

void move_up(void)
{ 	

	int i, j;
	int value, l ;

	for (j = 0; j < BOARD_NUM;j++)
	{

		value = 0;
		l = 0;
		for (i = 0; i<BOARD_NUM; i++)
		{
			if (matrix_2048[i][j] != 0)
			{
				if (value == 0)
				{
					// j直接取代value
					value = matrix_2048[i][j];
					matrix_2048[i][j] = 0;
				}
				else //value != 0
				{
					if (matrix_2048[i][j] == value)
					{
						value += matrix_2048[i][j];
						matrix_2048[l++][j] = value;
						value=0;
						matrix_2048[i][j] = 0;
					}
					else
					{
						//value前移，j取代value
						matrix_2048[l++][j] = value;
						value = matrix_2048[i][j];
						matrix_2048[i][j] = 0;
					}
				}
			}
		}

		if (value != 0)
		{
			matrix_2048[l++][j] = value;
		}
			
	}

}

void move_down(void)
{ 	
	//向左滑动的矩阵变换算法

	int i, j;
	int value, l ;

	for (j = 0; j< BOARD_NUM;j++)
	{

		value = 0;
		l = BOARD_NUM-1;
		for (i = BOARD_NUM-1; i>=0; i--)
		{
			if (matrix_2048[i][j] != 0)
			{
				if (value == 0)
				{
					// j直接取代value
					value = matrix_2048[i][j];
					matrix_2048[i][j] = 0;
				}
				else //value != 0
				{
					if (matrix_2048[i][j] == value)
					{
						value += matrix_2048[i][j];
						matrix_2048[l--][j] = value;
						value=0;
						matrix_2048[i][j] = 0;
					}
					else
					{
						//value前移，j取代value
						matrix_2048[l--][j] = value;
						value = matrix_2048[i][j];
						matrix_2048[i][j] = 0;
					}
				}
			}
		}

		if (value != 0)
		{
			matrix_2048[l--][j] = value;
		}
			
	}



	
}




int a=0;
void chang_matrix(int dir)
{
	if (dir == MOVE_LEFT)
	{
		move_left();
	}
	
	else if (dir == MOVE_RIGHT)
	{
		move_right();
	}
	
	else if (dir == MOVE_UP)
	{
		move_down();
	}
	
	else if (dir == MOVE_DOWN)
	{
		move_up();
	}
	a=1;			//a本来为0，滑动一次，a就变为1，代表动了一次
}
int end(void)		//当全部格子都填满了，就要判断格子周围还有没有相同的，如果有，那么就可以继续向左向右滑动。
{
	int i,j;
	if(get_zero_num()!=0)	//表示二维数组中至少还有一个空是0的，游戏还没结束，前面说了没有结束就返回0，结束了就返回1
	{
		return 0;
	}

	for (i = 0; i < BOARD_NUM-1 ; i++)
			{
				for (j = 0; j < BOARD_NUM-1; j++)
				{	

					if(i!=BOARD_NUM-1)			//判断列中有没有相同的
				    {
						if(matrix_2048[i][j] == matrix_2048[i+1][j])
						{
						    return 0;
						}
				    }
				    if(j!=BOARD_NUM-1)			//判断行中有没有相同的
				    {
						if(matrix_2048[i][j] == matrix_2048[i][j+1])
						{
							return 0;
						}
				    }
					else 
					{
					    return 1;				//返回1代表结束游戏
					}
					
				}
			}
	}


//2048游戏的主框架
void game_2048(void)
{
    int game_2048=end();
	
	//随机填充一个数字到矩阵，如填一个2
	fill_a_digtal_to_matrix();

	//画游戏界面，如果是0就调用红色，如果是2就调用2的图片
	lcd_draw_matrix();

	//game_over 本来就是0  代表启动
	while (!game_over)
	{
		//等待用户手指滑动
		int dir = get_finger_move_dir();

		//根据手指的滑动方向，变换矩阵
		chang_matrix(dir);


		//假如矩阵有变换，则随机填充一个数字
		//if (有变换)
		if(a==1)				//变量a在前面声明了为0，作为标志位，动了一次就变为1
		{
			fill_a_digtal_to_matrix();
			lcd_draw_matrix();
			game_over=end();	//判断游戏是否结束，没结束返回0
			a=0;
		}
		

		//判断游戏是否结束:  空白个数为0 && 相邻不相等
	}

}
